using System;
using System.Collections.Generic;
using System.Text;

namespace Engine
{
    public class CExpression
    {
        private int pos;
        public string expression;
        public string parserExpression(string express)
        {
            pos = 0;
            StringBuilder outPut = new StringBuilder();

            // Chuan hoa du lieu
            standard(express);
            
            // Qua trinh chuyen hoa
            while (pos < expression.Length)
            {
                char nextChar = next();
                if (nextChar == '=' || nextChar == '*' || nextChar == '(' || nextChar == ')' || nextChar == '+' || nextChar == ':' || nextChar == 'x' || nextChar == 'X' || nextChar == '?')
                {
                    if (pos > 0)
                    {
                        char preChar = this.expression[pos - 1];
                        if (preChar == '=' || preChar == '*' || preChar == '(' || preChar == ')' || preChar == '+' || preChar == ':' || preChar == 'x' || preChar == 'X' || preChar == '?')
                        {
                            outPut.Append(nextChar + " ");
                        }
                        else 
                        {
                            outPut.Append(" " + nextChar + " ");
                        }
                    }
                    else
                    {
                        outPut.Append(nextChar + " ");
                    }
                }
                else if (nextChar == '-')
                {
                    if (pos == 0)
                    {
                        outPut.Append(" " + nextChar);
                    }
                    else if (System.Char.IsNumber(this.expression[pos + 1]) && !System.Char.IsNumber(this.expression[pos - 1]))
                    {
                        outPut.Append(" " + nextChar);
                    }
                    else
                    {
                        outPut.Append(" " + nextChar + " ");
                    }
                }
                else
                {
                    outPut.Append(nextChar);
                }
                pos++;
            }

            if (outPut[outPut.Length - 1] == ' ') 
            {
                outPut.Remove(outPut.Length - 1, 1);
            }
            if (outPut[0] == ' ') 
            {
                outPut.Remove(0, 1);
            }
            
            return outPut.ToString();
        }

        public CExpression() 
        {
            pos = 0;
        }

        // Ham dung de doc ky tu tiep theo
        // Lay vi ky tu o vi tri hien tai
        private char next() 
        {
            if (pos >= this.expression.Length)
                return (char)0;
            return this.expression[pos];
        }

        // Ham dung de chuan hoa du cau hoi
        private string standard(string exp)
        {
            this.expression = exp;
            StringBuilder outPut = new StringBuilder();
            pos = 0;
            while (pos < this.expression.Length)
            {
                char currentChar = next();
                if (!System.Char.IsWhiteSpace(currentChar) && currentChar != 0)
                {
                    outPut.Append(currentChar + "");
                }
                pos++;
            }
            this.expression = outPut.ToString();
            pos = 0;
            
            return this.expression;
        }

        // Chuan hoa du lieu
        public string Standard(string exp) 
        {
            StringBuilder outPut = new StringBuilder();
            int position = 0;
            int numSpace = 0;
            while (position < exp.Length) 
            {
                char currentChar = exp[position];
                if (System.Char.IsWhiteSpace(currentChar) && currentChar != 0) 
                {
                    if (numSpace == 0) 
                    {
                        outPut.Append(currentChar + "");
                    }
                    numSpace++;
                }
                else if (!System.Char.IsWhiteSpace(currentChar) && currentChar != 0) 
                {
                    outPut.Append(currentChar + "");
                    numSpace = 0;
                }
                position++;
            }
            return outPut.ToString();
        }
    }
}
